//
//  LSYAlertSourceView.m
//  LSYReader
//
//  Created by sam on 2018/4/16.
//  Copyright © 2018年 okwei. All rights reserved.
//

#import "LSYAlertSourceView.h"

@implementation LSYAlertSourceView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setRules:(NSArray *)rules {
    _rules = rules;
    [self.tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rules.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSYRuleCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LSYRuleCell"];
    }
    LSYParseModel *rule = [_rules objectAtIndex:indexPath.row];
    if ([rule.host isEqualToString:_rule.host]) {
        cell.textLabel.textColor = [UIColor brownColor];
    } else {
        cell.textLabel.textColor = [UIColor blackColor];
    }
    cell.textLabel.text = rule.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(TDAlertView:didDismissWithButtonIndex:)]) {
        [self.delegate TDAlertView:nil didDismissWithButtonIndex:EVAlertViewButtonIndexCommit];
    }
    LSYParseModel *rule = [_rules objectAtIndex:indexPath.row];
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(selectedRule:)]) {
        [self.mDelegate selectedRule:rule];
    }
}

-(IBAction)refresh:(id)sender {
    [self.indicatorView startAnimating];
    self.button.hidden = YES;
    self.tableView.userInteractionEnabled = NO;
    if (self.refreshBlock) {
        self.refreshBlock();
    }
}

-(void)endRefresh {
    self.indicatorView.hidden = YES;
    [self.indicatorView stopAnimating];
    self.button.hidden = NO;
    self.tableView.userInteractionEnabled = YES;
}

@end
